<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<el-select v-model="status" clearable placeholder="请选择状态" style="width: 160px" @change="onStatusChange">
				<el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value"></el-option>
			</el-select>
			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key field="name" :field-list="[
				{
					label: '任务名称',
					value: 'name'
				},
				{
					label: '应用名称',
					value: 'appName'
				},
				{
					label: 'Topic',
					value: 'topic'
				}
			]" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>
	</cl-crud>
</template>

<script lang="ts" name="task-record" setup>
import { useCrud, useTable } from "@cool-vue/crud";
import { router, useCool } from "/@/cool";
import { watch } from "vue";
import { ref } from "vue";

const { service } = useCool();

//状态
const statusList = ref([
	{ label: "执行中", value: 0 },
	{ label: "成功", value: 1, color: "#67C23A" },
	{ label: "失败", value: 2, color: "#f56c6c" }
]);
//状态下拉搜索
const status = ref<any>(null);
const onStatusChange = () => {
	Crud.value?.refresh({ status: status.value, page: 1 });
};

// cl-table
const Table = useTable({
	columns: [
		{ prop: "id", label: "ID" },
		{ prop: "jobName", label: "任务名称" },
		{ prop: "topic", label: "Topic" },
		{ prop: "appName", label: "应用名称" },
		{
			prop: "status",
			label: "状态",
			dict: statusList.value,
			minWidth: 120
		},
		{ prop: "result", label: "结果", showOverflowTooltip: true },
		{ prop: "resultTime", label: "结果时间", width: 160 },
		{ prop: "createTime", label: "创建时间", sortable: "desc", width: 160 },
		{ prop: "updateTime", label: "更新时间", sortable: "custom", width: 160 }
	]
});

// cl-crud
const Crud = useCrud(
	{
		service: service.task.record
	},
	(app) => {
		app.refresh({ jobId: router.currentRoute.value.query.id });
	}
);

watch(
	() => router.currentRoute.value.query.id,
	() => {
		console.log("router.currentRoute.value.query.id:", router.currentRoute.value.query.id);
		Crud.value?.refresh({ jobId: router.currentRoute.value.query.id });
	}
);
</script>
